package com.loong.news.handler;

import com.loong.common.domain.BaseQuery;
import com.loong.common.domain.Pager;
import com.loong.common.enumerate.ResultCode;
import com.loong.common.exception.GlobalException;
import com.loong.common.executor.BatchExecutor;
import com.loong.common.utils.TransformUtils;
import com.loong.news.business.NewsCategoryBusiness;
import com.loong.news.common.backend.param.NewsCategoryParam;
import com.loong.news.common.backend.result.NewsCategoryResult;
import com.loong.news.mapper.NewsMapper;
import com.loong.news.model.NewsCategory;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Description: 处理新闻类目的handler
 * @Author: 刘群
 * @Date: 2020/10/14 13:49
 */
@Service
public class NewsCategoryHandler {

    @Resource
    private NewsCategoryBusiness categoryBusiness;

    @Resource
    private NewsMapper newsMapper;

    @Resource
    private BatchExecutor batchExecutor;

    /**
     * 新增新闻类别到数据库中
     * @param param
     */
    public void saveNewsCategory(NewsCategoryParam param){
        param.setCreateTime(new Date());
         this.operationNewsCategory("save",param);
    }

    /**
     * 更新新闻类别到数据库中
     * @param param
     */
    public void updateNewsCategory(NewsCategoryParam param){
        param.setLastUpdateTime(new Date());
         this.operationNewsCategory("update",param);
    }

    /**
     * 操作新闻类别
     * @param operationType 操作类型：新增、更新
     * @param param
     * @return
     */
    private void operationNewsCategory(String operationType, NewsCategoryParam param) {
        NewsCategory category = categoryBusiness.findBy("category", param.getCategory());
        if (ObjectUtils.isEmpty(category)) {//不存在该新闻类别则执行下面的操作
            if ("save".equals(operationType)) {
                //若是新增操作，则新增类别操作：
                categoryBusiness.save(TransformUtils.transform(param, NewsCategory.class));
            }
            if ("update".equals(operationType)) {
                //若是更新操作，则更新类别操作：
                categoryBusiness.updateByPrimaryKeySelective(TransformUtils.transform(param, NewsCategory.class));
            }
        }else {
            throw new GlobalException(ResultCode.PARAM_ERROR,"News category already exists");
        }
    }

    /**
     * 分页查询新闻类别
     * @return
     */
    public Pager<NewsCategoryResult> pageQuery(BaseQuery queryParam){
        Pager<NewsCategory> pager = categoryBusiness.pageQuery(queryParam, () -> categoryBusiness.findAll());
        return TransformUtils.transformPage(pager,NewsCategoryResult.class);
    }

    /**
     * 删除新闻类别
     * @param idList
     */
    public void deleteNewsCategory(List<Long> idList){
        batchExecutor.batchExecute(idList,id->id.toString(),(item)->{
            //根据新闻类别id查看新闻中是否有使用到这个类别
            Long countNum = newsMapper.findCountNumByCategoryId(item);
            if (countNum<1){
                categoryBusiness.deleteById(item);//没有使用到这个类别，因此可以直接删除。
            }else {
                throw new GlobalException("news category is using");
            }
        });
    }

}
